package com.test3;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListTest {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        //添加元素
        Students s1 = new Students("刘德华",20);
        Students s2 = new Students("黎明",22);
        Students s3 = new Students("张学友",18);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        System.out.println(list);//[Students{name='刘德华', age=20}, Students{name='黎明', age=22}, Students{name='张学友', age=18}]
        //删除
        list.remove(s1);//因为重写了equals所以删除成功
        System.out.println(list);//[Students{name='黎明', age=22}, Students{name='张学友', age=18}]
        //遍历
        //普通for
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        //foreach
        list.forEach(e -> System.out.println(e));
        //迭代器遍历
        Iterator it = list.iterator();
        while (it.hasNext()){
            Students s = (Students) it.next();
            System.out.println(s.toString());
        }
        //列表迭代器(有逆序，写法一样)
        ListIterator lit = list.listIterator();
        while (lit.hasNext()){
            Students s = (Students) lit.next();
            System.out.println(s.toString());
        }
        //判断
        System.out.println(list.contains(s2));//true
        System.out.println(list.isEmpty());//false
        //获取
        System.out.println(list.indexOf(s2));//0
    }
}
